//
//  LostTableViewCell.m
//  SmartCup
//
//  Created by Kendama on 2017/5/18.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LostTableViewCell.h"
@interface LostTableViewCell()

@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIImageView *switchIV;
@property (nonatomic, strong) UIView *line;
/** 开关按钮 **/
@property(nonatomic,strong) UIButton *switchButton;

@end

@implementation LostTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self masLayout];
        
    }
    return self;
}

- (void)masLayout {

    UIView *superView = self.contentView;
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView);
        make.width.equalTo(self.contentView);
        make.height.mas_equalTo(px1920Height(2));
    }];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView).with.offset(-px1920Height(85));
        make.left.equalTo(self.contentView).with.offset(px1080Width(48));
        make.right.equalTo(self.contentView.mas_centerX);
        make.height.mas_equalTo(px1920Height(170));
    }];
    
    
    [self.switchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView).with.offset(-px1920Height(85));
        make.right.equalTo(self.contentView).with.offset(-px1080Width(48));
        make.height.mas_equalTo(px1920Height(80));
        make.width.mas_equalTo(px1920Height(136));

    }];
    
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(superView).with.offset(px1920Height(85));
        make.left.equalTo(self.contentView).with.offset(px1080Width(48));
        make.right.equalTo(self.contentView).with.offset(-px1080Width(48));
        make.height.mas_equalTo(px1920Height(170));
    }];
    
    
}

- (void)setLost:(LostModel *)lost {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    [string attributeStrings:@[lost.startTime,@" 至 ",lost.endTime] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]],@[[UIFont systemFontOfSize:px1080Width(64)]]]];
    self.timeLabel.attributedText = string;
    
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] init];
    [string2 attributeStrings:@[@"重复：",lost.repeat] attributes:@[@[[UIColor colorWithHexString:@"#585D6D"]],@[[UIColor colorWithHexString:@"#585D6D"]]]];
    self.dateLabel.attributedText = string2;
    
    self.switchButton.selected = lost.switchMark;
}


- (void)setDict:(NSDictionary *)dict {
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] init];
    [string attributeStrings:@[dict[@"startTime"],@" 至 ",dict[@"endTime"]] attributes:@[@[[UIFont systemFontOfSize:px1080Width(64)]],@[[UIFont systemFontOfSize:px1080Width(34)]],@[[UIFont systemFontOfSize:px1080Width(64)]]]];
    self.timeLabel.attributedText = string;
    
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] init];
    [string2 attributeStrings:@[@"重复：",dict[@"repeat"]] attributes:@[@[[UIColor colorWithHexString:@"#585D6D"]],@[[UIColor colorWithHexString:@"#585D6D"]]]];
    self.dateLabel.attributedText = string2;
    
    if ([dict[@"switch"] isEqualToString:@"YES"]) {
        self.switchButton.selected = YES;
    } else {
        self.switchButton.selected = NO;
    }
}


- (void)didClickSwitchButton {
    self.switchButton.selected = !self.switchButton.selected;
    if (self.switchBlock) {
        if (self.switchButton.selected) {
            self.switchBlock(YES);
        } else {
            self.switchBlock(NO);
        }
    }
}


- (UILabel *)timeLabel
{
    if(!_timeLabel)
    {
        UIView *superView = self.contentView;
        _timeLabel = [[UILabel alloc]init];
        [superView addSubview:_timeLabel];
        [_timeLabel setFont:[UIFont systemFontOfSize:15]];
        [_timeLabel setTextColor:[UIColor colorWithHexString:@"#585D6D"]];

        
    }
    return _timeLabel;
}

- (UILabel *)dateLabel
{
    if(!_dateLabel)
    {
        UIView *superView = self.contentView;
        _dateLabel = [[UILabel alloc]init];
        [superView addSubview:_dateLabel];
        [_dateLabel setFont:[UIFont systemFontOfSize:15]];
        [_dateLabel setTextColor:[UIColor whiteColor]];

        
    }
    return _dateLabel;
}

- (UIImageView *)switchIV
{
    if(!_switchIV)
    {
        UIView *superView = self.contentView;
        _switchIV = [[UIImageView alloc]init];
        [superView addSubview:_switchIV];
        _switchIV.image = [UIImage imageNamed:@"button_on"];
        _switchIV.userInteractionEnabled = YES;
        
    }
    return _switchIV;
}

- (UIButton *)switchButton
{
    if(!_switchButton)
    {
        UIView *superView = self.contentView;
        _switchButton = [[UIButton alloc]init];
        [superView addSubview:_switchButton];
        [_switchButton setBackgroundImage:[UIImage imageNamed:@"button_off"] forState:UIControlStateNormal];
        [_switchButton setBackgroundImage:[UIImage imageNamed:@"button_on"] forState:UIControlStateSelected];
        [_switchButton addTarget:self action:@selector(didClickSwitchButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _switchButton;
}


- (UIView *)line
{
    if(!_line)
    {
        UIView *superView = self.contentView;
        _line = [[UIView alloc]init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#D0D3DC"];
        [superView addSubview:_line];
    }
    return _line;
}




+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"LostTableViewCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
